home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Light ROM 1
/
LIGHT-ROM 1 (Amiga Library Services)(1994).iso
/
ffdisks
/
d939.lha
/
ExtraCmds
/
source_etc.lha
/
src
/
Usage.c
< prev
Wrap
C/C++ Source or Header
|
1993-10-22
|
7KB
|
257 lines
/* --------------------------------- -------
* |\ | | | | | |.| | \| |/ /|\ |||||||
* | | | |/ | |\ |/ |/| |\ |/ | ? ---+--- =<
* | | | | | | | | | | | \qqqqqqqqq/
* --------------------------------- ~~~~~~~~~~~~~~~~
* Usage - Estimate disk space usage.
* Copyright (C) 1993 Torsten Poulin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* The author can be contacted by s-mail at
* Torsten Poulin
* Banebrinken 99, 2, 77
* DK-2400 Copenhagen NV
* DENMARK
*
* $Id: Usage.c,v 37.5 93/03/01 12:35:20 Torsten Rel $
* $Log: Usage.c,v $
* Revision 37.5 93/03/01 12:35:20 Torsten
* Changed all occurrences of "struct DosBase *" to "struct DosLibrary *"
*
* Revision 37.4 93/02/27 14:07:53 Torsten
* Corrected silly bug: I had "swapped" the values of 'rc' for failed
* calls to AllocVec() and ParseArgs() in entrypoint(), resulting
* in an out of memory message being produced by malformed command lines.
*
* Revision 37.3 93/02/26 11:49:03 Torsten
* Replaced the return code check in entrypoint() with
* a call to printErrorMsg(), unfortunately resulting in a 28 bytes
* bigger executable ;-(
* It now prints a reasonable error message if the global structure
* cannot be allocated.
*
* Revision 37.2 93/02/25 18:39:11 Torsten
* Tightened the code a bit.
*
* Revision 37.1 93/02/16 18:17:11 Torsten
* Initial revision.
*
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/dostags.h>
#include <dos/dosasl.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#ifdef __SASC
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#endif
#include <string.h>
#include "tastlib.h"
#include "usage_rev.h"
#define PROGNAME "Usage"
#define TEMPLATE "DIR/M,BRIEF/S,NOHEAD/S,NOTOTAL/S"
#define OPT_DIR 0
#define OPT_BRIEF 1
#define OPT_NOHEAD 2
#define OPT_NOTOTAL 3
typedef struct {
struct DosLibrary *DOSBase;
ULONG totalblocks;
ULONG totalbytes;
BOOL brief;
} Global;
LONG usage(Global *, UBYTE *);
char const versionID[] = VERSTAG;
char const copyright[] = "$COPYRIGHT:©1993 Torsten Poulin$";
LONG entrypoint(VOID)
{
struct DosLibrary *DOSBase;
struct RDArgs *args;
Global *global;
LONG arg[4];
LONG rc = RETURN_OK;
UBYTE **dir;
ULONG count;
if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
return RETURN_FAIL;
if (!(global = AllocVec(sizeof(Global), MEMF_PUBLIC | MEMF_CLEAR)))
rc = ERROR_NO_FREE_STORE;
else
{
global->DOSBase = DOSBase;
global->totalblocks = global->totalbytes = 0L;
arg[OPT_DIR] = arg[OPT_BRIEF] = arg[OPT_NOHEAD] = arg[OPT_NOTOTAL] = 0L;
if (!(args = ReadArgs(TEMPLATE, arg, NULL)))
rc = RETURN_FAIL;
else
{
global->brief = (BOOL) arg[OPT_BRIEF];
if (!(BOOL) arg[OPT_NOHEAD])
PutStr(" Blocks Bytes\n");
if (!arg[OPT_DIR])
rc = usage(global, "");
else
{
dir = (UBYTE **) arg[OPT_DIR];
for (count = 0; *dir && rc == RETURN_OK; dir++, count++)
rc = usage(global, *dir);
if (count > 1 && !(BOOL) arg[OPT_NOTOTAL])
{
PutStr(" --------- ----------\n");
MyPrintf(global, "%10ld %10ld\n",
global->totalblocks, global->totalbytes);
}
}
FreeArgs(args);
}
FreeVec(global);
}
rc = printErrorMsg(rc, PROGNAME, DOSBase);
CloseLibrary((struct Library *) DOSBase);
return rc;
}
LONG calc_usage(Global *global, UBYTE *dir, UBYTE *path,
ULONG *blocks, ULONG *bytes)
{
struct DosLibrary *DOSBase = global->DOSBase;
struct FileInfoBlock *m;
BPTR lock, oldlock;
LONG rc = RETURN_OK;
LONG type;
UBYTE *currentpath;
ULONG fblocks = 0L, fbytes = 0L;
ULONG dblocks = 0L, dbytes = 0L;
ULONG fudge;
if (!(m = AllocDosObject(DOS_FIB, NULL)))
rc = ERROR_NO_FREE_STORE;
else
{
if (!(currentpath = AllocVec(strlen(path)+1, MEMF_PUBLIC)))
rc = ERROR_NO_FREE_STORE;
else
{
if (!(lock = Lock(dir, SHARED_LOCK)))
rc = RETURN_ERROR;
else
{
oldlock = CurrentDir(lock);
if (Examine(lock, m))
{
type = m->fib_DirEntryType;
strcpy(currentpath, path);
if (type < 0 || type == ST_SOFTLINK)
{
PrintFault(ERROR_OBJECT_WRONG_TYPE, dir);
rc = RETURN_WARN;
}
else
{
while (ExNext(lock, m))
{
if (CheckSignal(SIGBREAKF_CTRL_C))
{
rc = ERROR_BREAK;
break;
}
type = m->fib_DirEntryType;
/* We don't want to handle soft links ;-) */
if (type != ST_SOFTLINK)
{
fblocks += fudge = m->fib_NumBlocks + 1; /* header (?) */
if (type < 0)
fbytes += m->fib_Size; /* it's a file */
else
{
/* it's a directory */
UBYTE *newpath;
if (!(newpath = AllocVec(strlen(currentpath)+108+2,
MEMF_PUBLIC)))
{
rc = ERROR_NO_FREE_STORE;
break;
}
strcpy(newpath, currentpath);
AddPart(newpath, m->fib_FileName,
strlen(currentpath) + 108 + 1);
dblocks = dbytes = 0L;
rc = calc_usage(global, m->fib_FileName, newpath,
&dblocks, &dbytes);
if (!global->brief)
MyPrintf(global, "%10ld %10ld %s\n",
dblocks ? dblocks : dblocks + fudge,
dbytes, newpath);
FreeVec(newpath);
fblocks += dblocks;
fbytes += dbytes;
if (rc == ERROR_BREAK || rc == ERROR_NO_FREE_STORE)
break;
}
}
}
*blocks = fblocks;
*bytes = fbytes;
}
}
CurrentDir(oldlock);
UnLock(lock);
}
FreeVec(currentpath);
}
FreeDosObject(DOS_FIB, m);
}
return rc;
}
LONG usage(Global *global, UBYTE *dir)
{
ULONG bl = 0L, by = 0L;
LONG rc;
if ((rc = calc_usage(global, dir, dir, &bl, &by)) == RETURN_OK)
MyPrintf(global, "%10ld %10ld %s\n", bl, by, dir);
global->totalblocks += bl;
global->totalbytes += by;
return rc;
}